package com.avic.rong.activity.mine;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.MyMemberCardAdapter;
import com.avic.rong.api.mine.ConverMemberApi;
import com.avic.rong.api.mine.MyMemberCardApi;
import com.avic.rong.bean.mine.MyMemberCardBean;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.wedgit.StringUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.dialog.CustomDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MemberCardActivity extends BaseActivity {

    @BindView(R.id.recycler_member)
    RecyclerView recycler_member;
    @BindView(R.id.bt_toview_member)
    Button bt_toview_member;
    @BindView(R.id.bt_activation_member)
    Button bt_activation_member;
    @BindView(R.id.ll_notmember)
    LinearLayout llNotMember;
    private ArrayList<MyMemberCardBean> mList = new ArrayList<>();
    private String loginToken;
    private MyMemberCardAdapter myMemberCardAdapter;
    private CustomDialog customDialog;

    //private String token = "w4/b+EQcZZnFlmpDOn6EJH49BIWDoQfvty5QOPgq/+YLF2xTVRyFnfxTic0l1spU1DKp8PMVM1YSixxW8qPiUHQ9CAb6QSHI";


    @Override
    protected int getContentViewId() {
        return R.layout.activity_member_card;
    }

    @Override
    protected void initView() {
        tvTitle.setText("我的会员卡");
        ivRight.setBackgroundResource(R.mipmap.slh);

        if(SharedPreferencesUtil.isLogin()){
            loginToken = SharedPreferencesUtil.getLoginToken();
        }

        myMemberCardAdapter = new MyMemberCardAdapter(this, mList);
        recycler_member.setAdapter(myMemberCardAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycler_member.setLayoutManager(linearLayoutManager);

        initData();
    }

    @Override
    protected void addListener() {
    }

    private void initData() {
        MyMemberCardApi memberCardApi = new MyMemberCardApi(this,myMemberCardListener );
        memberCardApi.setToken(loginToken);
        HttpManager.getInstance().doHttpDeal(memberCardApi);
    }

    HttpOnNextListener<BaseResultEntity> myMemberCardListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
           if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
               List<MyMemberCardBean> result = (List<MyMemberCardBean>) baseResultEntity.getResult();
               if (result!=null) {

                   mList.clear();
                   mList.addAll(result);
                   myMemberCardAdapter.notifyDataSetChanged();
               }else {
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                               llNotMember.setVisibility(View.VISIBLE);
                               recycler_member.setVisibility(View.INVISIBLE);
                       }
                   });
               }
           }else {
               Toast toast = Toast.makeText(MemberCardActivity.this, baseResultEntity.getCode() + "    " + baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
               toast.setGravity(Gravity.CENTER,0,0);
               toast.show();
           }
        }
    };

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, MemberCardActivity.class);
        return intent;
    }

    @OnClick({R.id.bt_toview_member, R.id.bt_activation_member})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_toview_member:
                startActivity(MemberDetailActivity.queryIntent(this));
                break;
            case R.id.bt_activation_member://点击激活账号服务
                showDialog();
                break;
        }
    }

    private void showDialog() {
        customDialog = new CustomDialog(this, R.layout.remember_dialog);
        customDialog.show();
        customDialog.setCanceledOnTouchOutside(false);

        EditText et_pwd = customDialog.findViewById(R.id.et_pwd);

        customDialog.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.cancel();
            }
        });
        customDialog.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = et_pwd.getText().toString().trim();
                if (StringUtil.isEmpty(trim)){
                    Toast toast = Toast.makeText(context, "激活密码不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                    return;
                }else {
                    ConverMemberApi converMemberApi = new ConverMemberApi(MemberCardActivity.this, converMemberListener);
                    converMemberApi.setArgument(loginToken, trim);
                    HttpManager.getInstance().doHttpDeal(converMemberApi);
                }
            }
        });
        customDialog.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.cancel();
            }
        });
    }

    HttpOnNextListener<BaseResultEntity> converMemberListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
                if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                    Toast toast = Toast.makeText(context,"兑换成功", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    mList.clear();
                    initData();//刷新列表
                }else {
                    Toast toast = Toast.makeText(context, baseResultEntity.getCode() + "    " + baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            customDialog.cancel();
        }
    };

}
